package shinesend.com.cn.level_3_cache;

import android.content.Context;
import android.support.v4.widget.ViewDragHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import shinesend.com.cn.level_3_cache.lib.PanoramaLoader;

/**
    三级缓存;
 */
public class MainActivity extends AppCompatActivity {

    /**
     *1级本地:--{
     *          内存Ram --->获取速度快,首先查找的对象;
     *                  --->如果没有的话
     *                          /----本地SDcard:
     *                              如果有就先把图片保存到Ram里--->添加图片
     *                              如果没有就从网络上直接下载--->保存到sdcard --->保存到ram里
     *
     *          }
     * --->有:就使用;
     * --->没有:如果没有就从(2级网络上)下载图片;
     *          |---下载-->保存到本地
     */

    private ListView lv;
    private MAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String[] lks = {
                "https://ss0.baidu.com/-Po3dSag_xI4khGko9WTAnF6hhy/image/h%3D200/sign=904ad5d7f0dcd100d29cff21428a47be/0b55b319ebc4b745e824c8ccc7fc1e178b8215b3.jpg",
                "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2206352547,3734402545&fm=116&gp=0.jpg",
                "http://img3.imgtn.bdimg.com/it/u=1822259355,3486537844&fm=21&gp=0.jpg",
                "http://photo.enterdesk.com/2009-7-2/enterdesk.com-C50508E76549F2AA830F0BD9E3C50EA8.jpg",
                "http://img2.imgtn.bdimg.com/it/u=183968428,1388159590&fm=11&gp=0.jpg",
                "http://img3.imgtn.bdimg.com/it/u=3923243397,3315212890&fm=21&gp=0.jpg",
                "http://img1.imgtn.bdimg.com/it/u=2231401493,877231736&fm=21&gp=0.jpg",
                "http://img4.imgtn.bdimg.com/it/u=792321926,1819378160&fm=21&gp=0.jpg",
                "http://img0.imgtn.bdimg.com/it/u=3975021897,3811586659&fm=21&gp=0.jpg",
        };

        lv = (ListView) findViewById(R.id.lv);
        mAdapter = new MAdapter(this,random(lks));
        lv.setAdapter(mAdapter);
    }
    private List<String> random(String[] lks) {

        List<String> res = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            res.add(lks[(new Random()).nextInt(lks.length)]);
        }
        return res;
    }
    class MAdapter extends BaseAdapter{

        private Context ctx;

        private  List<String> tv;

        private Animation animation;

        public MAdapter(Context ctx, List<String> tv) {
            this.ctx = ctx;
            this.tv = tv;
        }

        @Override
        public int getCount() {
            return tv.size();
        }

        @Override
        public Object getItem(int position) {
            return tv.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView==null){
                convertView = View.inflate(ctx,R.layout.list_item,null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.tv.setText(tv.get(position));
            PanoramaLoader.getInstance(MainActivity.this).loadInto(tv.get(position),viewHolder.iv);
            return convertView;
        }
    }
    public class ViewHolder {
        public TextView tv;
        public ImageView iv;
        public ViewHolder (View view) {
            tv = (TextView) view.findViewById(R.id.textview);
            iv = (ImageView) view.findViewById(R.id.imageview);
        }
    }
}
